package com.yycx.module.bus.provider.service.lc.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.module.bus.client.entity.hy.HyOrder;
import com.yycx.module.bus.client.entity.lc.LcZWFZYS;
import com.yycx.module.bus.client.entity.lc.LcZWPZBH;
import com.yycx.module.bus.client.entity.lc.LcZWPZFL;
import com.yycx.module.bus.client.entity.lc.LcZWPZK;
import com.yycx.module.bus.provider.mapper.lc.LcRemoteOrderMapper;
import com.yycx.module.bus.provider.service.lc.LcRemoteOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 浪潮财务凭证接口实现类
 *
 * @author flyme
 * @date 2020-08-17
 */
@DS("lc")
@Service
@Transactional(rollbackFor = Exception.class)
public class LcRemoteOrderServiceImpl extends BaseServiceImpl<LcRemoteOrderMapper, HyOrder> implements LcRemoteOrderService {
    //TODO 上线删掉
    //int currentYear = 2022;//DateUtil.thisYear();
    int currentYear = DateUtil.thisYear();

    @Resource
    private LcRemoteOrderMapper lcRemoteOrderMapper;

    @Override
    public Long getMaxPZNM() {
        return lcRemoteOrderMapper.getMaxPZNM(currentYear);
    }

    @Override
    public String getBMBH(String name, String DWBH) {
        return lcRemoteOrderMapper.getBMBH(name, DWBH);
    }

    @Override
    public String getDWBH(String name, String DWBH) {
        return lcRemoteOrderMapper.getDWBH(name.replaceAll("\\.", ""), DWBH);
    }

    @Override
    public String getDWBH2(String name, String DWBH) {
        return lcRemoteOrderMapper.getDWBH2(name.replaceAll("\\.", ""), DWBH);
    }

    @Override
    public String getSPBH(String name, String dw) {
        List<EntityMap> spbh2 = lcRemoteOrderMapper.getSPBH2(currentYear, name, dw);
        if (CollUtil.isEmpty(spbh2)) {
            return null;
        } else {
            List<String> tmpList = new ArrayList<>();
            for (EntityMap map : spbh2) {
                List<String> lshsxm_xmjc = StrUtil.split(MapUtil.getStr(map, "LSHSXM_XMJC", "").replace("，",","), ",");
                for (String s : lshsxm_xmjc) {
                    if (s.equals(name)) {
                        String tmp = map.get("LSHSXM_XMBH");
                        tmpList.add(tmp);
                    }
                }
            }
            if (FlymeUtils.isNotEmpty(tmpList) && tmpList.size() == 1) {
                return tmpList.get(0);
            } else {
                return name + "编码匹配到多个商品：" + spbh2.stream().map(a -> MapUtil.getStr(a, "LSHSXM_XMBH")).collect(Collectors.joining(","));
            }
        }
    }

    @Override
    public EntityMap getZWPZBH(String DWBH, String KJND, String KJQJ) {
        return lcRemoteOrderMapper.getZWPZBH(currentYear, DWBH, KJND, KJQJ);
    }

    @Override
    public void changeZWPZBH(String DWBH, String KJND, String KJQJ, int PZBH, String PZRQ) {
        lcRemoteOrderMapper.changeZWPZBH(currentYear, DWBH, KJND, KJQJ, PZBH, PZRQ);
    }

    @Override
    public void insertZWPZBH(List<LcZWPZBH> list) {
        lcRemoteOrderMapper.insertZWPZBH(currentYear, list);
    }

    @Override
    public void insertZWPZK(LcZWPZK lcZWPZK) {
        lcRemoteOrderMapper.insertZWPZK(currentYear, lcZWPZK);
    }

    @Override
    public void insertZWPZFL(List<LcZWPZFL> list) {
        lcRemoteOrderMapper.insertZWPZFL(currentYear, list);
    }

    @Override
    public void insertZWFZYS(List<LcZWFZYS> list) {
        lcRemoteOrderMapper.insertZWFZYS(currentYear, list);
    }

    @Override
    public List<EntityMap> getSPBHList(String companyCode, List<String> collect) {
        return lcRemoteOrderMapper.getSPBHList(currentYear, companyCode, collect);
    }
}
